package com.tcoiss.datafactory.controller;

import com.tcoiss.common.core.constant.HttpStatus;
import com.tcoiss.common.core.utils.poi.ExcelUtil;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.core.web.page.TableDataInfo;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import com.tcoiss.datafactory.domain.BusTable;
import com.tcoiss.datafactory.domain.vo.TablePageVO;
import com.tcoiss.datafactory.domain.vo.TableVO;
import com.tcoiss.datafactory.service.IBusTableService;
import com.tcoiss.datafactory.service.impl.TestService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 生成表结构业务Controller
 *
 * @author zw
 * @date 2021-04-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/table")
public class BusTableController extends BaseController {

    private final IBusTableService iBusTableService;

    @Autowired
    private TestService testService;

    /**
     * 查询业务表
     */
    @PreAuthorize(hasPermi = "bus:table:list")
    @GetMapping("/list")
    public TableDataInfo list(BusTable busTable) {
        startPage();
        List<BusTable> list = iBusTableService.queryList(busTable);
        return getDataTable(list);
    }

    /**
     * 查询业务表列表
     */
    //@PreAuthorize(hasPermi = "bus:table:list")
    @GetMapping("/getTables")
    public List<BusTable> getTables(BusTable busTable) {
        return iBusTableService.queryList(busTable);
    }


    /**
     * 导入业务表
     */
    @PreAuthorize(hasPermi = "bus:table:import")
    @Log(title = "业务表", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TableVO> util = new ExcelUtil<TableVO>(TableVO.class);
        List<TableVO> columns = util.importExcel(file.getInputStream());
        String message = iBusTableService.importTable(columns, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 导出业务表
     */
    @PreAuthorize(hasPermi = "bus:table:export")
    @Log(title = "业务表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusTable busTable) throws IOException {
        List<TableVO> list = iBusTableService.findList(busTable);
        ExcelUtil<TableVO> util = new ExcelUtil<TableVO>(TableVO.class);
        util.exportExcel(response, list, "业务表");
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<TableVO> util = new ExcelUtil<TableVO>(TableVO.class);
        util.importTemplateExcel(response, "表结构");
    }

    /**
     * 同步表结构
     */
    @PreAuthorize(hasPermi = "bus:table:syncJg")
    @Log(title = "业务表", businessType = BusinessType.SYNC)
    @PostMapping("/syncTableJg")
    public AjaxResult syncTableJg(@RequestBody BusTable table) {
        return toAjax(iBusTableService.syncTableJg(table) ? 1 : 0);
    }

    /**
     * 获取业务表配置详细信息,以及表的字段信息
     */
    @PreAuthorize(hasPermi = "bus:table:query")
    @GetMapping(value = "/{tableId}")
    public AjaxResult getInfo(@PathVariable("tableId") Long tableId) {
        return AjaxResult.success(iBusTableService.getBusTableById(tableId));
    }

    /**
     * 新增业务表配置
     */
    @PreAuthorize(hasPermi = "bus:table:add")
    @Log(title = "业务表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusTable busTable) {
        return toAjax(iBusTableService.saveTable(busTable) ? 1 : 0);
    }

    /**
     * 修改业务表
     */
    /*@PreAuthorize(hasPermi = "bus:table:edit")
    @Log(title = "业务表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusTable busTable) {
        return toAjax(iBusTableService.updateBusTableById(busTable) ? 1 : 0);
    }*/

    /**
     * 提交业务表配置，同步业务表结构，创建表并插入表数据
     *
     * @param busTable
     * @return
     */
    @PreAuthorize(hasPermi = "bus:table:edit")
    @Log(title = "业务表", businessType = BusinessType.UPDATE)
    @PutMapping("/updateBusTable")
    public AjaxResult editTable(@RequestBody BusTable busTable) {
        return toAjax(iBusTableService.updateBusTable(busTable) ? 1 : 0);
    }

    /**
     * 删除业务表配置
     */
    @PreAuthorize(hasPermi = "bus:table:remove")
    @Log(title = "业务表配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tableIds}")
    public AjaxResult remove(@PathVariable Long[] tableIds) {
        return toAjax(iBusTableService.removeTablesByIds(Arrays.asList(tableIds)) ? 1 : 0);
    }

    /**
     * 删除业务表配置
     */
    @PreAuthorize(hasPermi = "bus:table:remove")
    @Log(title = "业务表配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeChecked/{columnId}")
    public AjaxResult removeChecked(@PathVariable Long columnId) {
        return toAjax(iBusTableService.removeByColumnId(columnId) ? 1 : 0);
    }

    /**
     * 初始化业务表
     */
    @PreAuthorize(hasPermi = "bus:table:init")
    @Log(title = "业务表", businessType = BusinessType.INIT)
    @GetMapping("/initTable/{id}")
    public AjaxResult initTable(@PathVariable Long id) {
        return toAjax(iBusTableService.initTable(id) ? 1 : 0);
    }

    /**
     * 反初始化业务表
     */
    @PreAuthorize(hasPermi = "bus:table:init")
    @Log(title = "业务表", businessType = BusinessType.INIT)
    @GetMapping("/backInitTable/{id}")
    public AjaxResult backInitTable(@PathVariable Long id) {
        return toAjax(iBusTableService.backInitTable(id) ? 1 : 0);
    }
    /**
     * 查询预览表
     */
    @PreAuthorize(hasPermi = "bus:table:preview")
    @GetMapping("/preview")
    public TableDataInfo previewTable(TablePageVO pageVO) {
        //startPage();
        List<Map<String, Object>> list = iBusTableService.queryTableDate(pageVO);
        Integer total = iBusTableService.queryTableCount(pageVO);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(total);
        return rspData;
    }

    /**
     * 查询字段名列表
     */
    @PreAuthorize(hasPermi = "bus:table:preview")
    @GetMapping("/preview/getColumns/{tableId}")
    public AjaxResult getColumns(@PathVariable Long tableId) {
        return AjaxResult.success(iBusTableService.getColumns(tableId));
    }

    @PreAuthorize(hasPermi = "bus:table:preview")
    @GetMapping("/test")
    public AjaxResult test() {
        //testService.receiveBookJobRun();
        testService.insetTest();
        return AjaxResult.success();
    }

}
